package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.qo.ConsumptionQueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;

    @Autowired
    private IBusinessService businessService;

    @Autowired
    private IConsumptionItemService consumptionItemService;


    //处理查询所有结算单的请求 /consumption/list
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ConsumptionQueryObject qo){
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);//查出数据

        model.addAttribute("businesses",businessService.listAll());

        model.addAttribute("pageInfo",pageInfo);
        //model拿到数据后与jsp合并渲染成HTML格式，展现在页面上
        return "consumption/list";  // /WEB-INF/views/consumption/list.ftl
    }


    //处理删除请求 /consumption/delete?id=1
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id!=null) {
            consumptionService.delete(id);
        }
        return "redirect:/consumption/list";  //localhost/consumption/list
    }

    @RequiredPermission(name = "结算单新增/修改(无预约单)", expression = "consumption.saveOrUpdate")
    //处理保存或者修改请求请求 /consumption/saveOrUpdate  //参数在请求体中，操作input界面的提交功能
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Consumption consumption){
        if (consumption.getId()==null) {
            consumptionService.save(consumption);
        }else{
            consumptionService.update(consumption);
        }
        return "redirect:/consumption/list";  //localhost/consumption/list
    }

    @RequiredPermission(name = "结算单新增/修改(有预约单)", expression = "consumption.save")
    @RequestMapping("/save")
    public String save(Long aid){
        //修改预约单状态：消费中状态
        //新增预约的关联结算单：传入id/sn，查出预约单中的信息放入结算单中即可
        Long cid=consumptionService.save(aid);

        return "redirect:/consumption/input?id="+cid;
    }

    @RequestMapping("/input")
    public String input(Long id,Model model){

        if (id!=null){
            Consumption consumption = consumptionService.get(id);
            model.addAttribute("consumption",consumption);

            //查询结算单明细服务(通过sn来查出结算单)
            List<ConsumptionItem> items = consumptionItemService.queryByCno(consumption.getCno());
            model.addAttribute("items",items);
        }


        model.addAttribute("businesses",businessService.listAll());
        return "consumption/input";
    }
}
